package cn.linkstudy.controller;

import cn.linkstudy.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;

@RestController
@RequestMapping("/api/db")
public class DbController {
    @Autowired
    private DataSource dataSource;

    @GetMapping("/test")
    public Result testConnection() {
        try (Connection conn = dataSource.getConnection()) {
            return Result.success("数据库连接成功");
        } catch (Exception e) {
            return Result.error("数据库连接失败: " + e.getMessage());
        }
    }
}